package com.system.healthproject.model.admin.service.impl;

import com.system.healthproject.bean.AddHospitalParameter;
import com.system.healthproject.bean.ResultHospital;
import com.system.healthproject.bean.ResultHospitalIdName;
import com.system.healthproject.bean.ResultHospitalInfo;
import com.system.healthproject.mapper.DepartmentMapper;
import com.system.healthproject.mapper.HospitalMapper;
import com.system.healthproject.model.admin.service.HospitalService;
import com.system.healthproject.pojo.Department;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class HospitalServiceImpl implements HospitalService {
    @Autowired(required = false)
    private HospitalMapper hospitalMapper;
    @Autowired
    private DepartmentMapper departmentMapper;

    /**
     * 查询医院列表
     * @param hospitalName
     * @param hospitalLevel
     * @param hospitalAddress
     * @param hospitalNickname
     * @param createTime
     * @param currentPage
     * @param limit
     * @return
     */
    @Override
    public List<ResultHospital> selectAllHospital(String hospitalName, String hospitalLevel, String hospitalAddress,
                                                  String hospitalNickname, String createTime, Integer currentPage, Integer limit) {
        ResultHospital resultHospital = encapsulation(null,hospitalName,hospitalLevel,hospitalAddress,hospitalNickname,createTime);
        resultHospital.setPage((currentPage-1)*limit);
        resultHospital.setLimit(limit);
        return hospitalMapper.selectAllHospital(resultHospital);
    }

    /**
     * 统计医院条数
     * @param hospitalName
     * @param hospitalLevel
     * @param hospitalAddress
     * @param hospitalNickname
     * @param createTime
     * @param currentPage
     * @param limit
     * @return
     */
    @Override
    public Integer countHospital(String hospitalName, String hospitalLevel, String hospitalAddress, String hospitalNickname,
                                 String createTime, Integer currentPage, Integer limit) {
        ResultHospital resultHospital = encapsulation(null,hospitalName,hospitalLevel,hospitalAddress,hospitalNickname,createTime);
        return hospitalMapper.countHospital(resultHospital);
    }

    /**
     * 修改医生信息
     * @param hospitalId
     * @param hospitalName
     * @param hospitalLevel
     * @param hospitalAddress
     * @param hospitalNickname
     * @param createTime
     * @return
     */
    @Override
    public String updateHospital(
            Integer hospitalId,
            String hospitalName,
            String hospitalLevel,
            String hospitalAddress,
            String hospitalNickname,
            String createTime) {
            ResultHospital resultHospital =
                    encapsulation(
                            hospitalId,
                            hospitalName,
                            hospitalLevel,
                            hospitalAddress,
                            hospitalNickname,
                            createTime);
        return hospitalMapper.updateHospital(resultHospital)>0?"成功":"失败";
    }

    /**
     * 查询所有医院ID和名称
     * @return
     */
    @Override
    public List<ResultHospitalIdName> selectHospitalIdName() {
        return hospitalMapper.selectHospitalIdName();
    }

    /**
     * 添加医院
     * @param addHospitalParameter
     * @return
     */
    @Override
    public String addHospital(AddHospitalParameter addHospitalParameter) {
        addHospitalParameter.setCreateTime(new Date());
        return hospitalMapper.addHospital(addHospitalParameter)>0?"成功":"失败";
    }

    /**
     * 根据医院ID查询医院详细信息
     * @param hospitalId
     * @return
     */
    @Override
    public List<ResultHospitalInfo> selectByIdHospital(Integer hospitalId) {
        return hospitalMapper.selectByIdHospital(hospitalId);
    }

    /**
     * 根据医院ID删除
     * @param hospitalId
     * @return
     */
    @Override
    public String deleteHospital(Integer hospitalId) {
        //查询要删除的医院中有没有科室
        List<Department> list = departmentMapper.selectHospitalDepartment(hospitalId);
        if(list!=null && list.size()>0){
            return "医院中存在科室，不能删除";
        }else{
            return hospitalMapper.deleteHospital(hospitalId)>0?"成功":"失败";
        }
    }


    private ResultHospital encapsulation(
            Integer hospitalId,
            String hospitalName,
            String hospitalLevel,
            String hospitalAddress,
            String hospitalNickname,
            String createTime){
        ResultHospital paramHospital = new ResultHospital();
        if(hospitalId!=null){
            paramHospital.setHospitalId(hospitalId);
        } else paramHospital.setHospitalId(null);
        if(hospitalName!=null && !hospitalName.equals("")){
            paramHospital.setHospitalName(hospitalName);
        }else paramHospital.setHospitalName(null);
        if(hospitalLevel!=null && !hospitalLevel.equals("")){
            paramHospital.setHospitalLevel(hospitalLevel);
        }else paramHospital.setHospitalLevel(null);
        if(hospitalAddress!=null && !hospitalAddress.equals("")){
            paramHospital.setHospitalAddress(hospitalAddress);
        }else paramHospital.setHospitalAddress(null);
        if(hospitalNickname!=null && !hospitalNickname.equals("")){
            paramHospital.setHospitalNickname(hospitalNickname);
        }else paramHospital.setHospitalNickname(null);
        if(createTime!=null && createTime!=""){
            try {
                paramHospital.setCreateTime(new SimpleDateFormat("yyyy-MM-dd").parse(createTime));
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }else {
            paramHospital.setCreateTime(null);
        }
        return paramHospital;
    }
}
